/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.account.admin;

import ar.com.neolokura.travian.helper.troop.entity.TroopDetail;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author gustavo
 */
public class TroopParser {

    public TroopParser() {
    }

    public ArrayList<TroopDetail> getTroopDetails(String html) {

        html = html.replaceAll("\\r|\\n", "");
        //Gets all details tables
        ArrayList<String> detailsList = getDetailsList(html);


        ArrayList<TroopDetail> troopDetails = new ArrayList<TroopDetail>();
        // Creates a TroopDetail for each table created above:
        for (String table : detailsList) {
            TroopDetail troopDetail = getTroopDetailByTable(table);

            troopDetails.add(troopDetail);
        }


        return troopDetails;
    }

    private ArrayList<String> getDetailsList(String html) {
        //HARDCODE: Heree!!

        ArrayList<String> details = new ArrayList<String>();
        Pattern pattern = Pattern.compile("(?<=<table\\sclass=\"troop_details\"\\scellpadding=\"1\"\\scellspacing=\"1\">).*?(?=</table>)");
        Matcher matcher = pattern.matcher(html);

        while (matcher.find()) {
            if (!matcher.group().equals("")) {
                String detail = matcher.group();
                details.add(detail);
            }
        }
        return details;
    }

    private TroopDetail getTroopDetailByTable(String table) {
        //HARDCODE: Here!
        TroopDetail detail = new TroopDetail();

        // Gets HeadLine!
        Pattern pattern = Pattern.compile("(?<=<td[\\s]{0,999}class=\"troopHeadline\"[\\s]{0,999}colspan=\"[0-9]{0,999}\">[\\s]{0,999}<a[\\s]{0,999}href=\".{0,999}\">[\\s]{0,999}).*?(?=[\\s]{0,999}</a>[\\s]{0,999}</td>)");
        Matcher matcher = pattern.matcher(table);

        if (matcher.find()) {
            if (!matcher.group().equals("")) {
                detail.setTroopHeadline(matcher.group());
            }
        }

        // Gets Troop Owner
        pattern = Pattern.compile("(?<=<td[\\s]{0,999}class=\".{0,999}\">[\\s]{0,999}(<a[\\s]{0,999}href=\".{0,999}\">|<strong>)[\\s]{0,999}).*?(?=[\\s]{0,999}(</a>|</strong>)[\\s]{0,999}</td>[\\s]{0,999}<td)");
        matcher = pattern.matcher(table);

        if (matcher.find()) {
            if (!matcher.group().equals("")) {
                detail.setTroopOwner(matcher.group());
            }
        }

        // Gets Coords
        table = table.replaceAll("\\(|\\)", ""); // Gets rid of the brackets, easier regexp
        pattern = Pattern.compile("(?<=<span[\\s]{0,999}class=\"coordinate[X|Y]\">).*?(?=</span>)");
        matcher = pattern.matcher(table);

        for (int i = 0; i < 2 && matcher.find() && !matcher.group().equals(""); i++) {
            if(i == 0) {
                Integer x = Integer.parseInt(matcher.group());
                detail.setX(x);
            }
            if(i == 1) {
                Integer y = Integer.parseInt(matcher.group());
                detail.setY(y);
            }
        }
        
        // Gets Troops!
        pattern = Pattern.compile("(?<=<td[\\s]{0,999}class=\".{0,999}\">[\\s]{0,999}(<a[\\s]{0,999}href=\".{0,999}\">|<strong>)[\\s]{0,999}).*?(?=[\\s]{0,999}(</a>|</strong>)[\\s]{0,999}</td>[\\s]{0,999}<td)");
        matcher = pattern.matcher(table);

        if (matcher.find()) {
            if (!matcher.group().equals("")) {
                detail.setTroopOwner(matcher.group());
            }
        }

        
        
        return detail;
    }
    
    
}
